# Maintainer (MSYS2): Michael Hansen (zrax0111@gmail.com)
# Contributor: Zach Bacon <zachbacon@vba-m.com>

_variant=-${KF5_VARIANT:-shared}
source "$(dirname ${BASH_SOURCE[0]})"/../mingw-w64-PKGBUILD-common/kde-frameworks5
_kde_f5_init_package "${_variant}" "kdoctools"
pkgver=5.116.0
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'archlinux: kdoctools5'
)
pkgdesc="KF5 documentation framework using docbook (mingw-w64)"
license=('LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-extra-cmake-modules>=${pkgver}"
             "perl-URI"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
depends=()
if [ "${_variant}" = "-static" ]; then
  _kde_f5_add_depends "${_variant}" "${MINGW_PACKAGE_PREFIX}-qt5${_namesuff}"
else
  depends+=("${MINGW_PACKAGE_PREFIX}-qt5-base")
fi
_kde_f5_add_depends "${_variant}" "${MINGW_PACKAGE_PREFIX}-libxslt${_namesuff}"
_kde_f5_add_depends "${_variant}" "${MINGW_PACKAGE_PREFIX}-docbook-xsl${_namesuff}"
_kde_f5_add_depends "${_variant}" "${MINGW_PACKAGE_PREFIX}-ki18n-qt5${_namesuff}>=${pkgver}"
_kde_f5_add_depends "${_variant}" "${MINGW_PACKAGE_PREFIX}-karchive-qt5${_namesuff}>=${pkgver}"
groups=("${MINGW_PACKAGE_PREFIX}-kf5")
install="${_realname}-${MSYSTEM}.install"
source=("https://download.kde.org/stable/frameworks/${pkgver%.*}/${_basename}-${pkgver}.tar.xz"{,.sig}
        0001-kdoctools-relocate-datapath.patch)
sha256sums=('36a0a60e422907b2baead8b82cec56008240b7a950678294bb2e1df65e1a6bb3'
            'SKIP'
            '325a2476b69b5dabd61c65d899dcfdcd90c9408c0074dc5e6c9052007d99def2')
validpgpkeys=('53E6B47B45CEA3E0D5B7457758D0EE648A48B3BB') # David Faure <faure@kde.org>

prepare() {
  mkdir -p build-${MSYSTEM}${_variant}

  cd ${_basename}-${pkgver}
  patch -p1 -i "${srcdir}/0001-kdoctools-relocate-datapath.patch"
}

build() {
  local -a extra_config
  cd build-${MSYSTEM}${_variant}
  if [ "${_variant}" = "-static" ]; then
    extra_config+=( -DBUILD_SHARED_LIBS=NO )
  fi

  _kde_f5_build_env
    ${MINGW_PREFIX}/bin/cmake.exe ../${_basename}-${pkgver} \
      -DCMAKE_BUILD_TYPE=$(_kde_f5_CMAKE_BUILD_TYPE) \
      "${_kde_f5_KDE_INSTALL_DIRS[@]}" \
      -DCMAKE_INSTALL_DATAROOTDIR=share \
      -DPERL_EXECUTABLE=/usr/bin/perl \
      -DBUILD_QCH=OFF \
      -DBUILD_TESTING=OFF \
      -DECM_DIR=${MINGW_PREFIX}/share/ECM \
      "${extra_config[@]}" \
      -G'Ninja'
  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd build-${MSYSTEM}${_variant}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}"/share/kf5/kdoctools/customization/dtd/kdedbx45.dtd
  for file in "${pkgdir}${MINGW_PREFIX}"/share/kf5/kdoctools/customization/xsl/*.xml; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${file}"
  done
  for file in "${pkgdir}${MINGW_PREFIX}"/share/kf5/kdoctools/customization/*.xsl; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${file}"
  done
}
